$(function () {
    $("#jqGrid").jqGrid({
        url: '../actassign/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '流程名', name: 'processName', index: 'process_name', width: 80 ,formatter:getDictInfo},
			{ label: '条件概括', name: 'conddesc', index: 'conddesc', width: 80 }, 			
			{ label: '最小值', name: 'min', index: 'min', width: 80 }, 			
			{ label: '最大值', name: 'max', index: 'max', width: 80 }, 			
			{ label: '单位', name: 'unit', index: 'unit', width: 80 }, 			
		/*	{ label: '账号', name: 'username', index: 'username', width: 80 },
			{ label: '姓名', name: 'userdesc', index: 'userdesc', width: 80 },*/
			{ label: '创建时间', name: 'cdate', index: 'cdate', width: 80 }, 			
			{ label: '创建者', name: 'cusername', index: 'cusername', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.getDictList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showtable : false,
		title: null,
		actAssign: {},
        actAssigndtlList: {},
        dictList:{},
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable = false;
            vm.title = "新增";
			vm.actAssign = {};
            vm.actAssigndtlList=[];

            vm.actAssigndtlList.push({
                type:'0',
                username:''
            });
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.showtable = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            var params={"actAssigndtlList":vm.actAssigndtlList,
                "actAssign":vm.actAssign
            };
			var url = vm.actAssign.id == null ? "../actassign/save" : "../actassign/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(params),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../actassign/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../actassign/info/"+id, function(r){
				vm.actAssigndtlList=r.actAssigndtlList;
                vm.actAssign = r.actAssign;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showtable=false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},

        addCertificateItem: function(){
            this.actAssigndtlList.push({
                type:'0',
                username:''
            });
        },
        delCertificateItem: function(index){
            this.actAssigndtlList.splice(index,1);
        },
		popUser: function(dtlindex){
            popPage.popUserGrid();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择审批人",
                area: ['750px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popUser"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popUserGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
					vm.actAssigndtlList[dtlindex].userid=rowData.userId;
					vm.actAssigndtlList[dtlindex].username=rowData.username;

                    layer.close(index);
                }
            });
        },

        dtlInfo: function(){
            var id = getSelectedRow();
            if(id==null){
                return ;
            }
            vm.showtable = true;
            $("#jqGridTable").jqGrid({
                url: '../actassigndtl/list',
                datatype: "json",
                postData: {"id":id},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true },
                    { label: '审批级别', name: 'level', index: 'level', width: 150 },
                    { label: '类别', name: 'type', index: 'type', width: 150,formatter:function(v){
                        if(v=='0'){
                            return "主管"
                        }else if(v=='1'){
                            return "指定人"
                        }else {
                            return ""
                        }
                    } },
                    { label: '姓名', name: 'username', index: 'username', width: 150 },
                    { label: '创建时间', name: 'cdate', index: 'cdate', width: 180 },
                    { label: '创建者', name: 'cusername', index: 'cusername', width: 150 }
                ],
                viewrecords: true,
                height: 220,
                rowNum: 5,
                //rowList : [10,30,50],
                rownumbers: true,
                //rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                /*shrinkToFit:false,
                autoScroll: false,*/
                pager: "#jqGridPagerTable",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
				prmNames : {
				 page:"page",
				 rows:"limit",
				 order: "order"
				 },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridTable").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            $("#jqGridTable").jqGrid("clearGridData");
            $("#jqGridTable").jqGrid('setGridParam',{
                url: '../actassigndtl/list',
                datatype: "json",
                postData: {"id":id}
            }).trigger("reloadGrid");

        },
        getDictList: function(){
            var params = {
                dicttype:"spmc"
            }
            $.ajax({
                type: "POST",
                url: "../dictutils/getDictList",
                contentType: "application/json",
                data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.dictList = r.spmc;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        processChange: function(value){
            console.log(value);
            if(value=='spmc02'||value=='spmc03'||value=='spmc05'||value=='spmc06'||value=='spmc07'){
                vm.actAssign.conddesc="没有条件"  ;
                vm.actAssign.min="";
                vm.actAssign.max="";
               $('#min').val('');
                $('#max').val('');
                $('#conddesc').val('没有条件');
               $('#conddesc').attr("disabled",true);
                $('#min').attr("disabled",true);
                $('#max').attr("disabled",true);

                vm.actAssign.unit='';
            }else{
                $('#conddesc').attr("disabled",false);
                $('#min').attr("disabled",false);
                $('#max').attr("disabled",false);
                vm.actAssign.unit='天';
                vm.actAssign.conddesc='';
                $('#conddesc').val('');
            }
        },

	}
});

/*
function processChange(value){
    console.log(value);
    if(value=='spmc02'||value=='spmc03'||value=='spmc05'||value=='spmc06'||value=='spmc07'){
        vm.actAssign.conddesc="没有条件"  ;
        vm.actAssign.min="";
        vm.actAssign.max="";
      /!*  $('#min').val('');
        $('#max').val('');
        $('#conddesc').val('没有条件');*!/
        $('#conddesc').attr("disabled",true);
        $('#min').attr("disabled",true);
        $('#max').attr("disabled",true);

        vm.actAssign.unit='';
    }else{
        $('#conddesc').attr("disabled",false);
        $('#min').attr("disabled",false);
        $('#max').attr("disabled",false);
        vm.actAssign.unit='天';
        vm.actAssign.conddesc='';
    }
}*/
